import pygame
from config import config, uiconfig
from translate import l
from ui import src, weights, filedialog, msgbox
from ui.menu import menu
from tools import *
from pm import pm
import shutil
import hashlib


def encrypt(fpath: str, algorithm: str = "sha256") -> str:
    with open(fpath, 'rb') as f:
        return hashlib.new(algorithm, f.read()).hexdigest()


this_version = "1.0.0"
this_versioncode = 1

song_hash = ""
song_changeable = 1
cover_hash = ""
cover_show_obj = None
cover_set = 0


def song_choose_button():
    global song_hash
    info("ui/defaultset/defaultset", "choose song file")
    songfile = filedialog.open_song_file()
    if (songfile == ""):
        info("ui/defaultset/defaultset", "user cancel")
        return -1
    try:
        shutil.copyfile(songfile, menu.obj_path+"/"+"song.mp3")
    except IOError:
        err("ui/defaultset/defaultset", "permissions denied(IOError)")
        return -2
    info("ui/defaultset/defaultset", f"hashing...[{songfile}]")
    song_hash = encrypt(menu.obj_path+"/"+"song.mp3")
    info("ui/defaultset/defaultset", f"hash[{song_hash}]")


def cover_choose_button():
    global cover_hash, cover_show_obj, cover_set
    info("ui/defaultset/defaultset", "choose cover file")
    coverfile = filedialog.open_cover_file()
    if (coverfile == ""):
        info("ui/defaultset/defaultset", "user cancel")
        return -1
    try:
        shutil.copyfile(coverfile, menu.obj_path+"/"+"cover.png")
    except IOError:
        err("ui/defaultset/defaultset", "permissions denied(IOError)")
        return -2
    info("ui/defaultset/defaultset", f"hashing...[{coverfile}]")
    cover_hash = encrypt(menu.obj_path+"/"+"cover.png")
    info("ui/defaultset/defaultset", f"hash[{cover_hash}]")
    cover_show_obj = pygame.transform.scale(
        pygame.image.load(menu.obj_path+"/"+"cover.png"), (290, 163))
    cover_set = 1


filenamebox = weights.InputBox((200, 30, 400, 24))
bybox = weights.InputBox((200, 30+32*1, 400, 24))
songbybox = weights.InputBox((200, 30+32*2, 400, 24))
hardcodebox = weights.InputBox((200, 30+32*3, 80, 24))
hardlvbox = weights.FloatInputBox((340, 30+32*3, 80, 24), 20, 0)
hardlvspbox = weights.InputBox(
    (430, 30+32*3, 170, 24))  # for some special notes
bpmbox = weights.FloatInputBox((200, 30+32*4, 400, 24))
beatsfirstbox = weights.IntInputBox((200, 30+32*5, 190, 24), 8, 0)
beatssecondbox = weights.IntInputBox((410, 30+32*5, 190, 24), 8, 0)

songchoosebutton = weights.Button(
    (200, 30+32*7, 120, 32), l.choose_button, song_choose_button)
coverchoosebutton = weights.Button(
    (200, 30+32*8, 120, 32), l.choose_button, cover_choose_button)


def save_set():
    global song_hash, song_changeable

    def saves():
        global cover_hash, song_hash, song_changeable
        global this_version, this_versioncode
        manitemp = {
            "name": filenamebox.text,
            "version": this_version,
            "version_code": this_versioncode,
            "cover": cover_hash,
            "song": song_hash,
            "notes_by": bybox.text,
            "song_by": songbybox.text,
            "hard_code": hardcodebox.text,
            "hard_lv": hardlvbox.text,
            "hard_lv_sp": hardlvspbox.text,
            "bpm": bpmbox.text,
            "beats": [beatsfirstbox.text, beatssecondbox.text]
        }
        pm.save_manifest(menu.obj_path, manitemp)
        if (song_changeable == 1 and song_hash != ""):
            # should update
            menu.open_project(menu.obj_path)
    if (song_changeable == 1 and song_hash != ""):
        msgbox.add_msg(l.msg_save, l.msg_save_special_content, saves)
    else:
        msgbox.add_msg(l.msg_save, l.msg_save_content, saves)


save_button = weights.GlobalButton(
    None, save_set)  # set icon in change_info


def change_info(opens):
    save_button.icon = src.save_icon

    global song_changeable, song_hash, cover_set, cover_show_obj
    global this_version, this_versioncode

    filenamebox.text = opens[0]["name"]
    bybox.text = opens[0]["notes_by"]
    songbybox.text = opens[0]["song_by"]
    hardcodebox.text = opens[0]["hard_code"]
    hardlvbox.text = opens[0]["hard_lv"]
    hardlvspbox.text = opens[0]["hard_lv_sp"]
    bpmbox.text = opens[0]["bpm"]
    beatsfirstbox.text = opens[0]["beats"][0]
    beatssecondbox.text = opens[0]["beats"][1]
    this_version = opens[0]["version"]
    this_versioncode = opens[0]["version_code"]
    if (opens[0]["song"] == ""):
        song_changeable = 1
    else:
        song_changeable = 0
    if (opens[0]["cover"] != ""):
        cover_set = 1
        cover_show_obj = pygame.transform.scale(
            pygame.image.load(menu.obj_path+"/"+"cover.png"), (290, 163))
    else:
        cover_set = 0


def render(screen: pygame.Surface):
    global song_changeable, cover_set, song_hash, cover_set, cover_show_obj
    screen.blit(src.basic_font_24.render(
        l.file_name, config.config.ANTIALIAS_FONT, uiconfig.FONT_COLOR), (30, 30))
    filenamebox.update(screen)

    screen.blit(src.basic_font_24.render(
        l.by_who, config.config.ANTIALIAS_FONT, uiconfig.FONT_COLOR), (30, 30+32*1))
    bybox.update(screen)

    screen.blit(src.basic_font_24.render(
        l.song_by, config.config.ANTIALIAS_FONT, uiconfig.FONT_COLOR), (30, 30+32*2))
    songbybox.update(screen)

    screen.blit(src.basic_font_24.render(
        l.hard_code, config.config.ANTIALIAS_FONT, uiconfig.FONT_COLOR), (30, 30+32*3))
    hardcodebox.update(screen)
    screen.blit(src.basic_font_24.render(
        "Lv.", config.config.ANTIALIAS_FONT, uiconfig.FONT_COLOR), (300, 30+32*3-2))
    hardlvbox.update(screen)
    hardlvspbox.update(screen)

    screen.blit(src.basic_font_24.render(
        l.bpm_set, config.config.ANTIALIAS_FONT, uiconfig.FONT_COLOR), (30, 30+32*4))
    bpmbox.update(screen)

    screen.blit(src.basic_font_24.render(
        l.beats_set, config.config.ANTIALIAS_FONT, uiconfig.FONT_COLOR), (30, 30+32*5))
    beatsfirstbox.update(screen)
    beatssecondbox.update(screen)
    screen.blit(src.basic_font_24.render(
        "/", config.config.ANTIALIAS_FONT, uiconfig.FONT_COLOR), (395, 30+32*5))

    screen.blit(src.basic_font_24.render(
        l.song_file, config.config.ANTIALIAS_FONT, uiconfig.FONT_COLOR), (30, 30+32*7))
    if (song_changeable == 1):
        songchoosebutton.update(screen)

    screen.blit(src.basic_font_24.render(
        l.cover_file, config.config.ANTIALIAS_FONT, uiconfig.FONT_COLOR), (30, 30+32*8))
    coverchoosebutton.update(screen)
    if (cover_set == 1):
        screen.blit(cover_show_obj, (200, 30+32*9))

    save_button.update(screen)


def on_close():
    info("ui/defaultset/defaultset", "exit defaultset")
